<html>
<head>
	<script type="text/javascript">
		var aJaxURL			= "server-side/view/cartridge.action.php";			//server side folder url
		var list_aJaxURL	= "server-side/view/cartridge/list.action.php";		//server side folder url
		var upJaxURL		= "server-side/upload/file.action.php";				//server side folder url
		var tName			= "example";										//table name
		var fName			= "add-edit-form";									//form name
		var img_name		= "0.jpg";
		
		$(document).ready(function (){
			//ClearDB();
			
			LoadTable();
			
			/* Add Button ID, Delete Button ID */
			GetButtons("add_button", "delete_button", "", "", "clear_button");
			
			SetEvents("add_button", "delete_button", "check-all", tName, fName, aJaxURL);
		});
        
		function LoadTable(table){
			if(table == "ident_list"){
				var menuLength	= [[10], [10]];
				var local_id	= $("#prod_id").val();
				
				/* Table ID, aJaxURL, Action, Colum Number, Custom Request, Hidden Colum, Menu Array */
				GetDataTable("ident_list", list_aJaxURL, "get_list", 4, "local_id=" + local_id, 0, menuLength, 1, "asc");
			}else{
				/* Table ID, aJaxURL, Action, Colum Number, Custom Request, Hidden Colum, Menu Array */
                GetDataTable(tName, aJaxURL, "get_list", 6, "", 0, "", 0, "desc");
			}
		}
		
		function LoadDialog(form){
			if(form == fName){
				//ClearDB();
				
				var id = $("#prod_id").val();
				
				if(empty($("#page_resource").val()))
					$("#page_resource").val(0);
				
				if(empty($("#job_circle").val()))
					$("#job_circle").val(0);
					
				if(empty($("#min_amount").val()))
					$("#min_amount").val(1);
				
				$("#choose_button").button({
		            icons: {
		                primary: "ui-icon-arrowreturnthick-1-n"
		            }
	        	});
	        	
				$("#upload_button").button({
		            icons: {
		                primary: "ui-icon-arrowreturnthick-1-n"
		            }
	        	});
	        	
				var img_url	= $("#upload_img").attr("src");
		    	img_name	= img_url.split("\/")[4]; //Get image name element 4
		    	if(img_name != "0.jpg"){
		    		$("#choose_button").button("disable");
		    	}
	        	
				if(empty(id)){
					local_id = GetLocalID();
					$("#prod_id").val(local_id);
					CreateList(local_id);
				}else{
					/* Dialog Form Selector Name, Buttons Array */
					GetDialog(fName, 790, "auto", "");
		
					GetButtons("add_button_part", "delete_button_part");
    				LoadTable("ident_list");
    				SetEvents("add_button_part", "delete_button_part", "check-all-part", "ident_list", "add-edit-ident-form", list_aJaxURL);
				}
			}else{
				var buttons = {
			        "save": {
			            text: "დამატება",
			            id: "add-identity",
			            click: function () {
			            }
			        }
			    };
				GetDialog("add-edit-ident-form", 500, "auto", buttons);
			}
		}
		
	    // Add - Save
	    $(document).on("click", "#save-dialog", function () {
            var id	= $("#prod_id").val();
        	var check	= CheckList(id);
        	
        	if(check){
		    	param = new Object();
	
	            //Action
		    	param.act	= "save_prod";
	
	            //Main
		    	param.id	= $("#prod_id").val();
	
		    	param.cod	= $("#code").val().trim();
		    	param.pn	= $("#part_number").val().trim();
		    	param.n		= $("#name").val();
		    	
		    	param.cat 	= $("#prod_category").val();
		    	param.un 	= $("#unit").val();
	
		    	param.cy 	= $("#country").val();
		    	param.pr 	= $("#page_resource").val();
		    	param.jc 	= $("#job_circle").val();
		    	param.jed 	= $("#job_done_time").val();
		    	
		    	
		    	param.min	= $("#min_amount").val().trim();
	
			    param.c		= $("#comment").val();
			    
	            //Image
			    param.img 	= img_name;
				
				if(param.n == ""){
					alert("შეავსეთ პროდუქტის დასახელება!");
				}else if(param.cat == 0){
					alert("შეავსეთ კატეგორია!");
				}else if(param.ty == 0){
					alert("შეავსეთ ტიპი!");
				}else if(param.am == ""){
					alert("შეავსეთ რაოდენობა!");
				}else if(param.un == 0){
					alert("შეავსეთ ზომის ერთეული!");				
				}else  if(param.jed == ""){
					alert("შეავსეთ შესრულების სავარაუდო დრო!");				
				}else{
				    $.ajax({
				        url: aJaxURL,
					    data: param,
				        success: function(data) {
					        if(typeof(data.error) != "undefined"){
								if(data.error != ""){
									alert(data.error);
								}else{
									LoadTable(tName);
						        	CloseDialog(fName);
								}
							}
					    }
				    });
				}
        	}
		});
		
	    // Add - Save Identity
	    $(document).on("click", "#add-identity", function () {
	    	param = new Object();
	    	
            //Action
	    	param.act		= "save_ident";		    
		    
		    param.id		= $("#identity_id").val();	    	
		    param.l_id		= $("#prod_id").val();
		    
		    param.c			= $("#ident_category").val();
		    param.t			= $("#type").val();
		    param.a			= $("#amount").val();
		    
	    	if(param.c == 0){
				alert("შეავსეთ კატეგორია!");
			}else if(param.t == 0){
				alert("შეავსეთ ნაწილის ტიპი!");
			}else if(param.a == ""){
				alert("შეავსეთ რაოდენობა!");
			}else {
			    $.ajax({
			        url: list_aJaxURL,
				    data: param,
			        success: function(data) {
						if(typeof(data.error) != "undefined")
						{
							if(data.error != "")
							{
								alert(data.error);
							}else
							{
								LoadTable("ident_list");
				        		CloseDialog("add-edit-ident-form");
							}
						}
				    }
			    });
			}
		});
		
	    $(document).on("click", "#choose_button", function () {
		    $("#choose_file").click();
		});
		
	    $(document).on("change", "#choose_file", function () {
	    	var file		= $(this).val();	    
		    var name		= uniqid();
		    var path		= "../../media/uploads/images/production/";
		    
		    var ext = file.split('.').pop().toLowerCase();
	        if($.inArray(ext, ['gif','png','jpg','jpeg']) == -1) { //echeck file type
	        	alert('This is not an allowed file type.');
                this.value = '';
	        }else{
	        	img_name = name + "." + ext;
	        	$("#choose_button").button("disable");
	        	$.ajaxFileUpload({
	    			url: upJaxURL,
	    			secureuri: false,
	    			fileElementId: "choose_file",
	    			dataType: 'json',
	    			data:{
						act: "upload_file",
						path: path,
						file_name: name,
						type: ext
					},
	    			success: function (data, status){
	    				if(typeof(data.error) != 'undefined'){
    						if(data.error != ''){
    							alert(data.error);
    						}else{
    							$("#upload_img").attr("src", "media/uploads/images/production/" + img_name);
    						}
    					}
    				},
    				error: function (data, status, e)
    				{
    					alert(e);
    				}
    			});
	        }
		});
		
	    $(document).on("click", "#view_image", function () {
		    var src = $("#upload_img").attr("src");
		    $("#view_img").attr("src", src);
			var buttons = {
				"cancel": {
		            text: "დახურვა",
		            id: "cancel-dialog",
		            click: function () {
		                $(this).dialog("close");
		            }
		        }
		    };
	    	GetDialog("image-form", "auto", "auto", buttons);
		});
		
	    $(document).on("click", "#upload_img", function () {
		    var src = $("#upload_img").attr("src");
		    $("#view_img").attr("src", src);
			var buttons = {
				"cancel": {
		            text: "დახურვა",
		            id: "cancel-dialog",
		            click: function () {
		                $(this).dialog("close");
		            }
		        }
		    };
	    	GetDialog("image-form", "auto", "auto", buttons);
		});
				
	    $(document).on("click", "#delete_image", function () {
	    	var img_url	= $("#upload_img").attr("src");
	    	img_name	= img_url.split("\/")[4];	//Get image name element 4
	    	if(img_name != "0.jpg"){
		    	param = new Object();
		    	
	            //Action
		    	param.act		= "delete_file";
		    	
		    	param.path	 	= "../../media/uploads/images/production/";
			    param.file_name	= img_name;
			    var id			= $("#prod_id").val();
			    
	            $.ajax({
	                url: upJaxURL,
	                data: param,
	                success: function(data) {
	                    if (typeof(data.error) != "undefined") {
	                        if (data.error != "") {
	                            alert(data.error);
	                        } else {
	                        	$("#choose_button").button("enable");
	                        	$("#upload_img").attr("src", "media/uploads/images/production/0.jpg");
	                        	if(!empty(id)){
	                        		DeleteImage(id);
		                        }
	                        }
	                    }
	                }
	            });
			}
		});
		
	    function DeleteImage(prod_id) {
            $.ajax({
                url: aJaxURL,
                data: "act=delete_image&id=" + prod_id,
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                    	if (data.error != "") {
                            alert(data.error);
                        } else{
                        	img_name = "0.jpg";                        	
                        }
                    }
                }
            });
        }
        
	    function CreateList(local_id) {
            $.ajax({
                url: aJaxURL,
                data: "act=create_list&id=" + local_id,
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        } else{
        					/* Dialog Form Selector Name, Buttons Array */
        					GetDialog(fName, 790, "auto", "");
        		
        					GetButtons("add_button_part", "delete_button_part");
	        				LoadTable("ident_list");
	        				SetEvents("add_button_part", "delete_button_part", "check-all-part", "ident_list", "add-edit-ident-form", list_aJaxURL);
                    	}
                	}
                }
            });
        }
        
        function CheckList(id) {
            var check = false;
            $.ajax({
                url: aJaxURL,
                async: false, //r-value
                data: "act=check_list&id=" + id,
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        } else {
                            check = true;
                        }
                    }
            	}
        	});
            return check;
        }
        
        function ClearDB() {
            $.ajax({
                url: aJaxURL,
                data: "act=clear_db",
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        }
                    }
                }
            });
        }
        
		/**
		* Get next auto incremented id
		* @return {int}   Returns a next id
		*/
		function GetLocalID(){
			var local_id;
			$.ajax({
		        url: aJaxURL,
        		async: false, //r-value
			    data: "act=get_local_id",
		        success: function(data) {
					if(typeof(data.error) != "undefined"){
						if(data.error != ""){
							alert(data.error);
						}else{
							local_id = data.local_id;
						}
					}
			    }
		    });
	      	return local_id;
		}
		
		$(document).on("click", "#clear_button", function () {
	    	param = new Object();
	    	
			//Action
	    	param.act	= "get_file_list";
			param.path	= "../../media/uploads/images/production/";
			var file_list = "";
            $.ajax({
                url: upJaxURL,
                data: param,
                async: false, //r-value
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        } else {
                        	file_list = data.file_list;
                        }
                    }
                }
            });
            	    	
            //Action
	    	param.act	= "clear";	    	
	    	param.file	= file_list;
	    	
	    	$.ajax({
                url: aJaxURL,
                data: param,
                async: false, //r-value
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        } else {
                            alert("Database Clear Successfully Completed");
                            file_list = data.file_list;
                        }
                    }
                }
            });
	    	file_list = $.parseJSON(file_list);
            
	    	for (var i = 0; i < file_list.length; i++) {		    	
		    	param.act		= "delete_file";		    	
			    param.file_name	= file_list[i];
			    			    
	            $.ajax({
	                url: upJaxURL,
	                data: param,
	                success: function(data) {
	                    if (typeof(data.error) != "undefined") {
	                        if (data.error != "") {
	                            alert(data.error);
	                        }
	                    }
	                }
	            });
		    }
		});
		
		$(document).on("keydown", "#code", function (event) {
			if (event.keyCode == $.ui.keyCode.ENTER){
            	$("#prod_name").focus();
				event.preventDefault();					//Clear Dialog Default <b>keypress<b> Event
            }
		});
    </script>
</head>

<body>
    <div id="dt_example" class="ex_highlight_row">
        <div id="container">
            <div id="dynamic">
            	<h2 align="center">კარტრიჯები</h2>
	        	<div id="button_area">
	        		<button id="add_button">დამატება</button><button id="delete_button">წაშლა</button>
	        	</div>
                <table class="display" id="example">
                    <thead>
                        <tr id="datatable_header">
                            <th>ID</th>
                            <th class="min">კოდი</th>
                            <th style="width: 100%">დასახელება</th>
                            <th class="min">ზომის ერთეული</th>
                            <th class="min">კატეგორია</th>
                            <th class="min">მინ. რაოდენობა</th>
                            <th class="check">#</th>
                        </tr>
                    </thead>
                    <thead>
                        <tr class="search_header">
                            <th class="colum_hidden">
                            	<input type="text" name="search_id" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_code" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_name" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_size" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_category" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_minimal" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                            	<input type="checkbox" name="check-all" id="check-all">
                            </th>
                        </tr>
                    </thead>
                </table>
	        	<div id="bottom_button_area">
	        		<button id="clear_button" class="right">clear</button>
	        	</div>
            </div>
            <div class="spacer">
            </div>
        </div>
    </div>
    
    <!-- jQuery Dialog -->
    <div id="add-edit-form" class="form-dialog" title="პროდუქტები">
    	<!-- aJax -->
	</div>
	
    <!-- jQuery Dialog -->
    <div id="image-form" class="form-dialog" title="პროდუქციის სურათი">
    	<img id="view_img" src="media/uploads/images/production/0.jpg">
	</div>
</body>
</html>